import { createSlug } from '@/core/utils/slug' import toTitleCase from '@/core/utils/toTitleCase' import productSearchApi from '@/lib/product/api/productSearchApi' import variantSearchApi from '@/lib/product/api/variantSearchApi' import _ from 'lodash-contrib' import { create } from 'xmlbuilder' export async function getServerSideProps({ res, query }) { const titleContent = 'Indoteknik.com: B2B Industrial Supply & Solution' const descriptionContent = 'Temukan pilihan produk B2B Industri & Alat Teknik untuk Perusahaan, UMKM & Pemerintah dengan lengkap, mudah dan transparan.' const { page } = query const limit = 5000 const queries = { limit, page: page.replace('.xml', ''), priceFrom: 1, orderBy: 'popular', fq: 'image_s:["" TO *]' } const products = await variantSearchApi({ query: _.toQuery(queries) }) const productItems = [] products.response.products.forEach((product) => { const productUrl = createSlug('/shop/product/variant/', product.name, product.id, true) const productId = product.code != '' ? product.code : product.id const regexHtmlTags = /(<([^>]+)>)/gi product.description = product.description?.replace(regexHtmlTags, ' ').trim() const defaultProductDescription = 'Indoteknik.com menawarkan berbagai produk industri, konstruksi, dan teknik terpercaya. Temukan mesin industri, peralatan listrik, alat pengukur, dan banyak lagi. Pengalaman berbelanja mudah dengan deskripsi produk lengkap, spesifikasi teknis, dan gambar jelas. Pembayaran aman, pengiriman cepat ke seluruh Indonesia. Solusi lengkap untuk kebutuhan Kantor, Industri & Teknik Anda.' if (!product.description) { product.description = defaultProductDescription } const availability = 'in_stock' const item = { 'g:id': { '#text': productId }, 'g:title': { '#text': toTitleCase(product.name) }, 'g:description': { '#text': product.description }, 'g:link': { '#text': productUrl }, 'g:image_link': { '#text': product.image }, 'g:condition': { '#text': 'new' }, 'g:availability': { '#text': availability }, 'g:brand': { '#text': product.manufacture?.name || '' }, 'g:price': { '#text': `${Math.round(product.lowestPrice.price * 1.11)} IDR` } } if (product.lowestPrice.discountPercentage > 0) { item['g:sale_price'] = { '#text': `${Math.round(product.lowestPrice.priceDiscount * 1.11)} IDR` } } productItems.push(item) }) const googleMerchant = { rss: { '@xmlns:g': 'http://base.google.com/ns/1.0', '@version': '2.0', channel: { title: { '#text': `` }, link: { '#text': process.env.SELF_HOST }, description: { '#text': `` }, item: productItems } } } res.setHeader('Content-Type', 'text/xml;charset=iso-8859-1') res.write(create(googleMerchant).end()) res.end() return { props: {} } } export default function GoogleMerchantPage() { return null }